<?php
/**
 * 《mac》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/WifiUtil.class.php');

class UserMac extends WifiUtil {
	public $tbl = Constant::tbl_wifi_mac;
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '已经审核',
		// '3' => '停止',
	
		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',

		'gw_id' => '节点',
		'uid' => '帐号',
		'mac' => 'MAC',
		'ua' => 'ua',
		
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		
		'gw_id' => '节点',
		'uid' => '帐号',

		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		// $controller->assign('t1', strtotime('-1 month'));

		$controller->display($request, 'wifi/mac/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		
		$where = " WHERE 1=1";
		
		if (!empty($name_s)) {
			$s = '%' . $name_s . '%';
			$where .= sprintf(" AND (name LIKE '%s' OR mac LIKE '%s' OR ua LIKE '%s' OR content LIKE '%s') ", $s, $s, $s, $s);
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
		
		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'wifi/mac/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'wifi/mac/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$mac = $request['mac'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, mac, name, content, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $mac, $name, $content, $_SESSION['id']);
		debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);
		
		$row['brand'] = $this->_brand($row['ua']);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/mac/edit');
		return true;
	}
	
	function _brand($s) {
		// $s = "Dalvik/2.1.0 (Linux; U; Android 5.1.1; SM-G9250 Build/LMY47X)";
		
		if (preg_match("/^(Dalvik)/i", $s)) {
			
			$a = preg_split("/;/i", $s);
			
			// print_r($a);
			
			$b = trim(end($a));
			
			$a = preg_split("/\s/i", $b);
			
			return $a[0];
		}
		
		return '';
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$username = $request['username'];
		$mac = $request['mac'];

		$name = addslashes($request['name']);
		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', username = '%s', name = '%s', content = '%s', ";
		$fmt .= "status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $username, $name, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/mac/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		if (!empty($request['id'])) {
			$row = $this->_view($request['id']);
		} elseif (!empty($request['mac'])) {
			$row = $this->_view_mac($request['mac']);
		}

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'wifi/mac/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row = $this->_view_t($row);
	
		return $row;
	}
	
	function _view_mac($mac) {
		$row = Common::x($this->tbl, 'mac', $mac);
		if (!$row) {
			return false;
		}
	
		$row = $this->_view(null, $row);
	
		return $row;
	}
	
	function _update($mac, $ua) {
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = '%s', modifytime = NOW() WHERE status <> '2' AND mac = '%s'";
		$sql = sprintf($fmt, $this->tbl, $ua, $mac);
		echo $sql . "\n";
		$db->query($sql);
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		// $controller->assign('t1', time());
		// $controller->assign('t2', strtotime('+1 day'));
	
		$controller->display($request, 'wifi/mac/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
	
		$fmt = "SELECT %s AS a, COUNT(mac) AS c FROM %s a ";
		
		$sql = sprintf($fmt, $this->s, $this->tbl);
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);

		return true;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-') {
			$px = 'emp';
		}
	
		$row = $this->_name_t($row, $px);
	
		return $row;
	}

	function test($controller, $request) {
		$this->pretreat($controller);
		
		require_once 'Log.class.php';
		
		$aa = new Log();
		
		$db = newdb("abc");
		$fmt = "SELECT * FROM %s WHERE emp = '' OR emp is NULL";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			
			if ($ret = $aa->_by_mac($db, $row['mac'])) {
				$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s' WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $ret['office'], $ret['dept'], $ret['emp'], $row['id']);
				echo $sql . "\n";
				$db->query($sql);
			}
		}

		logdate($controller, $request);
		return true;
	}
	
	function t($controller, $request) {
		$this->pretreat($controller);
		
		logdate($controller, $request);
		return true;
	}
}
?>